package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Quotation", "/quotation", "/QuotationServlet", "/quotationServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class QuotationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public QuotationServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<QuotationModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/quotationNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			QuotationModel query = new QuotationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(new QuotationModel());

			QuotationModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("quotation", obj);
				getServletContext().getRequestDispatcher("/quotationEdit.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			QuotationModel query = new QuotationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(new QuotationModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				response.sendRedirect("quotationList.jsp");
			} else {
				// TODO fail
			}
		}

		else if (mode.equalsIgnoreCase("view")) {

			QuotationModel query = new QuotationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(new QuotationModel());

			QuotationModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("quotation", obj);
				getServletContext().getRequestDispatcher("/quotationView.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			QuotationModel obj = new QuotationModel();

//			String id_ref = request.getParameter("id_ref");
//			if (id_ref != null && !id_ref.isEmpty()) {
//				obj.setIdRef(Integer.parseInt(id_ref));
//			}

			String id_project = request.getParameter("id_project");
			if (id_project != null && !id_project.isEmpty()) {
				obj.setIdProject(Integer.parseInt(id_project));
			}

			String name = request.getParameter("name");
			obj.setName(name);

			String code = request.getParameter("code");
			obj.setCode(code);

			String description = request.getParameter("description");
			obj.setDescription(description);

			String id_author = request.getParameter("id_author");
			if (id_author != null && !id_author.isEmpty()) {
				obj.setIdAuthor(Integer.parseInt(id_author));
			}

			String id_approver = request.getParameter("id_approver");
			if (id_approver != null && !id_approver.isEmpty()) {
				obj.setIdApprover(Integer.parseInt(id_approver));
			}

			String issue_date_date = request.getParameter("issue_date_date");
			String issue_date_time = request.getParameter("issue_date_time");
			if (issue_date_date != null && !issue_date_date.isEmpty() && issue_date_time != null && !issue_date_time.isEmpty()) {
				obj.setIssueDate(DateTimeUtil.parseDateTime(issue_date_date, issue_date_time));
			} else {
				obj.setIssueDate(DateTimeUtil.parseDate(issue_date_date));
			}

			String remark = request.getParameter("remark");
			obj.setRemark(remark);

			String id_currency = request.getParameter("id_currency");
			if (id_currency != null && !id_currency.isEmpty()) {
				obj.setIdCurrency(Integer.parseInt(id_currency));
			}

			String total = request.getParameter("total");
			if (total != null && !total.isEmpty()) {
				obj.setTotal(Double.parseDouble(total));
			}

			String management_fee = request.getParameter("management_fee");
			if (management_fee != null && !management_fee.isEmpty()) {
				obj.setManagementFee(Double.parseDouble(management_fee));
			}

			String vat_rate = request.getParameter("vat_rate");
			if (vat_rate != null && !vat_rate.isEmpty()) {
				obj.setVatRate(Double.parseDouble(vat_rate));
			}

			String grand_total = request.getParameter("grand_total");
			if (grand_total != null && !grand_total.isEmpty()) {
				obj.setGrandTotal(Double.parseDouble(grand_total));
			}

			String id_type = request.getParameter("id_type");
			if (id_type != null && !id_type.isEmpty()) {
				obj.setIdType(Integer.parseInt(id_type));
			}

			String status = request.getParameter("status");
			if (status != null && !status.isEmpty()) {
				obj.setStatus(Integer.parseInt(status));
			}

			String notification = request.getParameter("notification");
			if (notification != null && !notification.isEmpty()) {
				obj.setNotification(Integer.parseInt(notification));
			}

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(new QuotationModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
					//getServletContext().getRequestDispatcher("/quotationNew.jsp").forward(request, response);
					String urlIdProject = "quotationList.jsp?idProject=" + obj.getIdProjectToString();
					response.sendRedirect(urlIdProject);
					//getServletContext().getRequestDispatcher("/quotationNew.jsp"+urlIdProject).forward(request, response);
				} else {
					// TODO : processing error here
				}
			} else {

				// invalid input

				request.setAttribute("quotation", obj);
				getServletContext().getRequestDispatcher("/quotationInput.jsp").forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			QuotationModel query = new QuotationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(new QuotationModel());

			QuotationModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

//				String id_ref = request.getParameter("id_ref");
//				if (id_ref != null && !id_ref.isEmpty()) {
//					obj.setIdRef(Integer.parseInt(id_ref));
//				}

				String id_project = request.getParameter("id_project");
				if (id_project != null && !id_project.isEmpty()) {
					obj.setIdProject(Integer.parseInt(id_project));
				}

				String name = request.getParameter("name");
				obj.setName(name);

				String code = request.getParameter("code");
				obj.setCode(code);

				String description = request.getParameter("description");
				obj.setDescription(description);

				String id_author = request.getParameter("id_author");
				if (id_author != null && !id_author.isEmpty()) {
					obj.setIdAuthor(Integer.parseInt(id_author));
				}

				String id_approver = request.getParameter("id_approver");
				if (id_approver != null && !id_approver.isEmpty()) {
					obj.setIdApprover(Integer.parseInt(id_approver));
				}

				String issue_date_date = request.getParameter("issue_date_date");
				String issue_date_time = request.getParameter("issue_date_time");
				if (issue_date_date != null && !issue_date_date.isEmpty() && issue_date_time != null && !issue_date_time.isEmpty()) {
					obj.setIssueDate(DateTimeUtil.parseDateTime(issue_date_date, issue_date_time));
				} else {
					obj.setIssueDate(DateTimeUtil.parseDate(issue_date_date));
				}

				String remark = request.getParameter("remark");
				obj.setRemark(remark);

				// P.V.Van modified this for commenting currency.
				String id_currency = request.getParameter("id_currency");
				if (id_currency != null && !id_currency.isEmpty()) {
					obj.setIdCurrency(Integer.parseInt(id_currency));
				}
				
				String total = request.getParameter("total");
				if (total != null && !total.isEmpty()) {
					obj.setTotal(Double.parseDouble(total));
				}
				
				String management_fee = request.getParameter("management_fee");
				if (management_fee != null && !management_fee.isEmpty()) {
					obj.setManagementFee(Double.parseDouble(management_fee));
				}
				
				String vat_rate = request.getParameter("vat_rate");
				if (vat_rate != null && !vat_rate.isEmpty()) {
					obj.setVatRate(Double.parseDouble(vat_rate));
				}
				
				String grand_total = request.getParameter("grand_total");
				if (grand_total != null && !grand_total.isEmpty()) {
					obj.setGrandTotal(Double.parseDouble(grand_total));
				}
				
				String id_type = request.getParameter("id_type");
				if (id_type != null && !id_type.isEmpty()) {
					obj.setIdType(Integer.parseInt(id_type));
				}

				String status = request.getParameter("status");
				if (status != null && !status.isEmpty()) {
					obj.setStatus(Integer.parseInt(status));
				}

				String notification = request.getParameter("notification");
				if (notification != null && !notification.isEmpty()) {
					obj.setNotification(Integer.parseInt(notification));
				}

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
						//getServletContext().getRequestDispatcher("/quotationNew.jsp").forward(request, response);
						String urlIdProject = "quotationList.jsp?idProject=" + obj.getIdProjectToString();
						response.sendRedirect(urlIdProject);
						//getServletContext().getRequestDispatcher("/quotationNew.jsp"+urlIdProject).forward(request, response);
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("quotation", obj);
					getServletContext().getRequestDispatcher("/quotationEdit.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			QuotationModel query = new QuotationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(new QuotationModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				getServletContext().getRequestDispatcher("/quotationList.jsp").forward(request, response);
			} else {
				// TODO fail
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			QuotationModel obj = new QuotationModel();

			// TODO set name here

			GenericDal<QuotationModel, QuotationModelExt> dal = new GenericDal<QuotationModel, QuotationModelExt>(new QuotationModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		}
	}

}